package com.craftplay.dao;

import com.craftplay.model.Place;
import com.craftplay.model.User;
import com.craftplay.model.UserKey;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Service;

import java.util.List;

@Mapper
public interface PlaceMapper {
    @Delete({
            "delete from place",
            "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(int id);

    @Select({
            "select",
            "id, user_id, name, address, img_url",
            "from place",
            "where user_id = #{userId,jdbcType=INTEGER}"
    })
   @Results( {
        @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
        @Result(column = "user_id", property = "userId", jdbcType = JdbcType.INTEGER),
        @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
        @Result(column = "address", property = "address", jdbcType = JdbcType.VARCHAR),
        @Result(column = "img_url", property = "imgUrl", jdbcType = JdbcType.VARCHAR)
    })
    List<Place> selectByUserId(int userId);


    @Select({
            "select",
            "id, user_id, name, address, img_url",
            "from place",
            "where id = #{id,jdbcType=INTEGER}"
    })
    @Results( {
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "user_id", property = "userId", jdbcType = JdbcType.INTEGER),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "address", property = "address", jdbcType = JdbcType.VARCHAR),
            @Result(column = "img_url", property = "imgUrl", jdbcType = JdbcType.VARCHAR)
    })
    Place selectByPlaceId(int id);

    @Insert({
            "insert into place (user_id,",
            "name, address, img_url)",
            "values (#{userId,jdbcType=INTEGER}, ",
            "#{name,jdbcType=VARCHAR},#{address,jdbcType=VARCHAR},#{imgUrl,jdbcType=VARCHAR})"
    })
    int insert(Place place);

    @Update({
            "update place",
            "set name = #{name,jdbcType=VARCHAR},",
            "address = #{address,jdbcType=VARCHAR},",
            "img_url = #{imgUrl,jdbcType=VARCHAR}",
            "where id = #{id,jdbcType=INTEGER}"
    })
    int updatePlaceInfo(@Param("id") int id, @Param("name") String name, @Param("address") String address, @Param("imgUrl") String imgUrl);
}
